package com.huike.web.controller.business;

import com.huike.business.domain.TbBusiness;
import com.huike.business.service.TbBusinessService;
import com.huike.clues.domain.vo.AssignmentVo;
import com.huike.clues.service.TbClueService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商机Controller
 * @date 2021-04-25
 */
@Api(tags = "商机相关接口")
@RestController
@RequestMapping("/business")
public class TbBusinessController extends BaseController {

    @Resource
    private TbBusinessService tbBusinessService;

    @Resource
    private TbClueService tbClueService;


    @ApiOperation("查询商机列表")
    @PreAuthorize("@ss.hasPermi('business:business:list')")
    @GetMapping("/list")
    public TableDataInfo<List<TbBusiness>> list(TbBusiness tbBusiness,HttpServletRequest req){

        if((!StringUtils.isEmpty(req.getParameter("params[beginCreateTime]")))&&
                (!StringUtils.isEmpty(req.getParameter("params[endCreateTime]")))) {
            tbBusiness.setBeginCreateTime(req.getParameter("params[beginCreateTime]"));
            tbBusiness.setEndCreateTime(req.getParameter("params[endCreateTime]"));
        }
        List<TbBusiness> list = tbBusinessService.selectTbBusinessList(tbBusiness);
        return getDataTablePage(list);
    }



    @ApiOperation("查询公海池")
    @PreAuthorize("@ss.hasPermi('business:business:pool')")
    @GetMapping("/pool")
    public TableDataInfo<List<TbBusiness>> pool(TbBusiness tbBusiness,HttpServletRequest req){

        startPage();
        if((!StringUtils.isEmpty(req.getParameter("params[beginCreateTime]")))&&
                (!StringUtils.isEmpty(req.getParameter("params[endCreateTime]")))) {
            tbBusiness.setBeginCreateTime(req.getParameter("params[beginCreateTime]"));
            tbBusiness.setEndCreateTime(req.getParameter("params[endCreateTime]"));
        }
        List<TbBusiness> list = tbBusinessService.selectTbBusinessPool(tbBusiness);
        return getDataTable(list);
    }


    @ApiOperation("获取商机详细信息")
    @PreAuthorize("@ss.hasPermi('business:business:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult<TbBusiness> getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbBusinessService.selectTbBusinessById(id));
    }

    @ApiOperation("新增商机")
    @PreAuthorize("@ss.hasPermi('business:business:add')")
    @Log(title = "商机", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult<Integer> add(@RequestBody TbBusiness tbBusiness)
    {
        if(!tbClueService.checkCluePhoneExis(tbBusiness.getPhone())) return error("手机号已存在");
        return toAjax(tbBusinessService.insertTbBusiness(tbBusiness));
    }

    @ApiOperation("修改商机")
    @PreAuthorize("@ss.hasPermi('business:business:edit')")
    @Log(title = "商机", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult<Integer> edit(@RequestBody TbBusiness tbBusiness)
    {
        return toAjax(tbBusinessService.updateTbBusiness(tbBusiness));
    }

    @ApiOperation("删除商机")
    @PreAuthorize("@ss.hasPermi('business:business:remove')")
    @Log(title = "商机", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Integer> remove(@PathVariable Long[] ids)
    {
        return toAjax(tbBusinessService.deleteTbBusinessByIds(ids));
    }

    @ApiOperation("商机分配")
    @PreAuthorize("@ss.hasPermi('business:business:assignment')")
    @Log(title = "批量分配", businessType = BusinessType.UPDATE)
    @PutMapping("/assignment")
    public AjaxResult<String> assignment(@RequestBody AssignmentVo assignmentVo)
    {
        return AjaxResult.success(tbBusinessService.assign(assignmentVo.getIds(),assignmentVo.getUserId()));
    }

    @ApiOperation("批量捞取")
    @PreAuthorize("@ss.hasPermi('business:business:gain')")
    @Log(title = "批量捞取", businessType = BusinessType.UPDATE)
    @PutMapping("/gain")
    public AjaxResult<String> gain(@RequestBody AssignmentVo assignmentVo)
    {
        return AjaxResult.success(tbBusinessService.gain(assignmentVo.getIds(),assignmentVo.getUserId()));
    }

    @ApiOperation("踢回公海")
    @PreAuthorize("@ss.hasPermi('business:business:back')")
    @Log(title = "踢回公海", businessType = BusinessType.UPDATE)
    @PutMapping("/back/{id}/{reason}")
    public AjaxResult back(@PathVariable("id") Long id, @PathVariable("reason")String reason)
    {
        return AjaxResult.success(tbBusinessService.backPool(id,reason));
    }

}